package com.whfp.coky.ui.entry.vm

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.hjq.toast.ToastUtils
import com.tencent.mmkv.MMKV
import com.whfp.coky.databinding.ActivityActivationBinding
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseViewModel
import com.whfp.coky.ui.entry.model.ActivationBea
import com.whfp.coky.utils.InfoTool
import com.whfp.coky.utils.LocationTool
import com.whfp.coky.utils.LogUtil

/**
 *
 * 1、点击激活，获取定位权限进行定位
 * 2、定位成功后，跳转扫码
 * 3、扫码成功后，加密成字符串提交注册激活
 * 4、激活成功后，保存服务器地址
 *
 * created by coky on 2022/3/1
 */
class ActivationVM: BaseViewModel<ActivityActivationBinding>() {

    var activationLD = MutableLiveData<ActivationBea>()

    override fun observe(activity: BaseActivity<*, *>, owner: LifecycleOwner) {
        super.observe(activity, owner)
        activationLD.observe(owner, Observer {
            LogUtil.e("监听到了....${it.linkSys}")
            MMKV.defaultMMKV().putString(Constants.ADDR_URL_ACT,it.linkSys)
        })
    }

   fun doActivation(result:String){
//        激活
        LogUtil.e("激活：$result")
       val map: HashMap<String, String> = HashMap()
       map["ctDeviceCode"] = InfoTool.buildPhoneMac()
       map["msgBody"] = result
       launch({httpUtil.doRegister(body = map)},activationLD,true)
   }

    fun doLocation(context: Context){
        LocationTool(context).doLoc()
    }


}